"use strict";
cc._RF.push(module, 'f7e76pyAUJH9bgMbvwvCd08', 'GameScene');
// Script/GameScene.ts

"use strict";
/**
 * 怀旧模式
 */
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
Object.defineProperty(exports, "__esModule", { value: true });
var MapControl = require("./MapControl");
var Define_1 = require("./SdkTools/Define");
var gamePrefabMgr_1 = require("./SdkTools/gamePrefabMgr");
var gameShareAdsVideo_1 = require("./SdkTools/gameShareAdsVideo");
var PlayData_1 = require("./SdkTools/PlayData");
var storage_1 = require("./SdkTools/storage");
var Utils_1 = require("./SdkTools/Utils");
var timeOver_1 = require("./timeOver");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var GameScene = /** @class */ (function (_super) {
    __extends(GameScene, _super);
    function GameScene() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        _this.mapcontrol = null;
        _this.map_png = null;
        _this.xuankuang = null;
        _this.taskTimeStr = null;
        _this.tishinode = null;
        _this.answerPanel = null;
        _this.titlestr = null;
        _this.getAnswerBtn = null;
        _this.leveldata = null;
        _this.levelnum = 0;
        _this.tasktime = 0;
        _this.answerNum = 0; //需要找出的数量
        return _this;
    }
    // LIFE-CYCLE CALLBACKS:
    GameScene.prototype.onLoad = function () {
        this.initGame();
    };
    GameScene.prototype.initGame = function () {
        var _this = this;
        gameShareAdsVideo_1.default.Instance.recording_Video_Start();
        this.getAnswerBtn.active = !0;
        this.mapcontrol.resetMap();
        var self = this;
        var _data = storage_1.default.Instance.getCheckPoint_diff();
        if (PlayData_1.default.Instance.checkoutNum == Define_1.default.maxCheckNum_huaijiu) {
            PlayData_1.default.Instance.checkoutNum = Define_1.default.maxCheckNum_huaijiu - 1;
        }
        this.levelnum = PlayData_1.default.Instance.checkoutNum + 1; //Number(_data[3])  + 1
        var png_path = 'huaijiu/level' + this.levelnum + '/png' + this.levelnum;
        cc.resources.load(png_path, cc.SpriteFrame, function (err, assert) {
            if (err)
                return console.log('err=', err);
            self.map_png.spriteFrame = assert;
        });
        var data_path = 'huaijiu/level' + this.levelnum + '/level' + this.levelnum;
        cc.resources.load(data_path, function (err, assert) {
            if (err)
                return console.log('err=', err);
            self.leveldata = assert;
            self.setLevelData();
            _this.tasktime = Define_1.default.zhaobutong_maxTime;
            _this.taskTimeStr.string = '00:00';
            _this.unscheduleAllCallbacks();
            _this.schedule(function () {
                if (PlayData_1.default.Instance.ispause)
                    return;
                self.tasktime--;
                if (self.tasktime <= 0) {
                    //let sc = self.top.getComponent(TopUi)
                    //sc.cutSchedule()
                    var c_str = Utils_1.default.secondsFormat(self.tasktime, false);
                    self.taskTimeStr.string = c_str;
                    self.unscheduleAllCallbacks();
                    self.showGameOver(0);
                }
                else {
                    var c_str = Utils_1.default.secondsFormat(self.tasktime, false);
                    self.taskTimeStr.string = c_str;
                }
            }, 1, 99999, 0);
        });
        this.setTiShi();
        this.resetAnswerPng();
    };
    GameScene.prototype.start = function () {
        cc.director.emit('game_bg1');
    };
    /**
     * 设置关卡
     */
    GameScene.prototype.setLevelData = function () {
        var points = this.leveldata.json.point;
        this.answerNum = points.length;
        var img_w = 1316;
        var img_h = 870;
        this.map_png.node.removeAllChildren();
        this.titlestr.string = this.leveldata.json.title;
        //console.log('打印设置关卡', points)
        for (var index = 0; index < points.length; index++) {
            var point = points[index];
            var newnode = new cc.Node();
            var sp1 = newnode.addComponent(cc.Sprite);
            sp1.spriteFrame = null; // this.xuankuang
            sp1.type = cc.Sprite.Type.SLICED;
            sp1.sizeMode = cc.Sprite.SizeMode.CUSTOM;
            // newnode.x = point.x - img_w/2 + point.w/2  
            // newnode.y = img_h - point.y - point.h/2 - img_h/2
            //console.log('打印位置', index, points[index])
            newnode.x = point.x;
            newnode.y = point.y;
            newnode.width = point.w;
            newnode.height = point.h;
            newnode.parent = this.map_png.node;
            //console.log('打印节点数据', this.map_png.node)
            newnode.name = '' + index;
            newnode.on(cc.Node.EventType.TOUCH_END, this.selectAnswer, this);
        }
    };
    /**
     * 展示结束
     */
    GameScene.prototype.showGameOver = function (type) {
        var newnode = cc.instantiate(gamePrefabMgr_1.default.Instance.uiPre[gamePrefabMgr_1.uiPreName.timeOver]);
        var sc = newnode.getComponent(timeOver_1.default);
        sc.setType(type);
        newnode.parent = cc.director.getScene();
    };
    /**
     * 选择答案
     * @param event
     */
    GameScene.prototype.selectAnswer = function (event) {
        //console.log('选择答案',event)
        var sp = event.target.getComponent(cc.Sprite);
        if (sp.spriteFrame == null) {
            sp.spriteFrame = this.xuankuang;
            this.answerNum--;
            this.checkAnswerOver(event.target.name);
            var newtexiao = cc.instantiate(gamePrefabMgr_1.default.Instance.uiPre[gamePrefabMgr_1.uiPreName.texiao]);
            newtexiao.parent = sp.node;
            cc.tween(newtexiao)
                .delay(1)
                .removeSelf()
                .start();
        }
        else {
            cc.tween(sp.node)
                .to(0.1, { scale: 0.9 })
                .to(0.1, { scale: 1 })
                .start();
        }
    };
    /**
     * 设置提示道具数量
     */
    GameScene.prototype.setTiShi = function () {
        var num = cc.find('tinum', this.tishinode).getComponent(cc.Label);
        num.string = '' + storage_1.default.Instance.getTiShi();
    };
    /**
     * 提示按钮点击
     */
    GameScene.prototype.tishiBtnClick = function () {
        if (storage_1.default.Instance.getTiShi() <= 0) {
            var newnode = cc.instantiate(gamePrefabMgr_1.default.Instance.uiPre[gamePrefabMgr_1.uiPreName.addTiShi]);
            newnode.parent = cc.director.getScene();
        }
        else {
            storage_1.default.Instance.setTiShi(storage_1.default.Instance.getTiShi() - 1);
            this.setTiShi();
            var pointdata = this.leveldata.json.point;
            var length = pointdata.length;
            for (var index = 0; index < length; index++) {
                var _name = '' + index;
                var sp = cc.find(_name, this.map_png.node).getComponent(cc.Sprite);
                if (sp.spriteFrame == null) {
                    this.answerNum--;
                    this.checkAnswerOver(_name);
                    sp.spriteFrame = this.xuankuang;
                    var newtexiao = cc.instantiate(gamePrefabMgr_1.default.Instance.uiPre[gamePrefabMgr_1.uiPreName.texiao]);
                    newtexiao.parent = sp.node;
                    cc.tween(newtexiao)
                        .delay(1)
                        .removeSelf()
                        .start();
                    break;
                }
            }
        }
    };
    /**
     * 获取答案
     
    getAnswerBtnClick(){
        var self = this
        gameShareAdsVideo.Instance.motivational_Video_Show(()=>{
            self.getAnswerBtn.active = !1
            let points = self.leveldata.json.point
            for (let index = 0; index < points.length; index++) {
                let point = points[index];
                
                let newnode = new cc.Node()
                let sp1 = newnode.addComponent(cc.Sprite)
                sp1.spriteFrame = self.xuankuang
                sp1.type = cc.Sprite.Type.SLICED
                sp1.sizeMode = cc.Sprite.SizeMode.CUSTOM
    
                newnode.x = point.x
                newnode.y = point.y
                newnode.width = point.w
                newnode.height = point.h
                newnode.zIndex = -128
                newnode.parent = self.map_png.node
            }
        }, ()=>{})
    }
*/
    /**
     * 获取答案按钮点击 - 修改为倒计时增加60秒
     */
    GameScene.prototype.getAnswerBtnClick = function () {
        var self = this;
        gameShareAdsVideo_1.default.Instance.motivational_Video_Show(function () {
            // self.getAnswerBtn.active = !1;     点击后按钮消失
            self.addTime(60); // 添加60秒到当前的游戏倒计时
        }, function () {
            // 如果用户选择不观看广告或广告加载失败，你可以选择不执行任何操作或显示一些消息
            console.log('广告未完成，时间未添加');
        });
    };
    /**
     * 增加指定秒数到当前的倒计时
     * @param seconds 要添加的秒数
     */
    GameScene.prototype.addTime = function (seconds) {
        this.tasktime += seconds;
        // 更新显示的倒计时
        var formattedTime = Utils_1.default.secondsFormat(this.tasktime, false);
        this.taskTimeStr.string = formattedTime;
        this.updateCountdown();
    };
    /**
     * 更新倒计时的调度器
     */
    GameScene.prototype.updateCountdown = function () {
        var _this = this;
        this.unscheduleAllCallbacks(); // 先取消之前的所有调度
        this.schedule(function () {
            if (PlayData_1.default.Instance.ispause)
                return; // 如果游戏处于暂停状态，则不减少时间
            _this.tasktime--;
            if (_this.tasktime <= 0) {
                _this.taskTimeStr.string = Utils_1.default.secondsFormat(0, false);
                _this.unscheduleAllCallbacks();
                _this.showGameOver(0); // 时间耗尽，展示游戏结束
            }
            else {
                _this.taskTimeStr.string = Utils_1.default.secondsFormat(_this.tasktime, false);
            }
        }, 1, this.tasktime, 0); // 每秒更新一次，直到倒计时结束
    };
    /**
     * 判断是否作答完成
     */
    GameScene.prototype.checkAnswerOver = function (pngid) {
        var _this = this;
        this.setAnswerPng(pngid);
        //全部作答完成
        if (this.answerNum <= 0) {
            var _data = storage_1.default.Instance.getCheckPoint_diff();
            if (_data[3][PlayData_1.default.Instance.checkoutNum] == 0) {
                _data[3][PlayData_1.default.Instance.checkoutNum] = 1;
                PlayData_1.default.Instance.checkoutNum++;
                if (_data[3][PlayData_1.default.Instance.checkoutNum] != 0 && _data[3][PlayData_1.default.Instance.checkoutNum] != 1) {
                    _data[3][PlayData_1.default.Instance.checkoutNum] = 0;
                    storage_1.default.Instance.setHightScore(storage_1.default.Instance.getHightScore() + 1);
                }
                storage_1.default.Instance.setCheckPoint_diff(_data);
            }
            else {
                PlayData_1.default.Instance.checkoutNum++;
            }
            this.unscheduleAllCallbacks();
            this.scheduleOnce(function () {
                _this.showGameOver(3);
            }, 1);
        }
    };
    /**
     * 设置下面 答案图
     */
    GameScene.prototype.setAnswerPng = function (pngid) {
        //console.log('设置图', pngid)
        var index = 10 - this.answerNum - 1;
        var sp = cc.find('sp' + index + '/icon_shield', this.answerPanel).getComponent(cc.Sprite);
        var num = Number(pngid) + 1;
        var png_path = 'huaijiu/level' + this.levelnum + '/' + num;
        cc.resources.load(png_path, cc.SpriteFrame, function (err, assert) {
            if (err)
                return console.log('err=', err);
            sp.spriteFrame = assert;
        });
        var points = this.leveldata.json.point;
        var str = cc.find('sp' + index + '/str', this.answerPanel).getComponent(cc.Label);
        var point = points[num - 1];
        str.string = '' + point.name;
    };
    GameScene.prototype.resetAnswerPng = function () {
        for (var index = 0; index < 10; index++) {
            var sp = cc.find('sp' + index + '/icon_shield', this.answerPanel).getComponent(cc.Sprite);
            sp.spriteFrame = null;
            var str = cc.find('sp' + index + '/str', this.answerPanel).getComponent(cc.Label);
            str.string = '';
        }
    };
    // update (dt) {}
    /**
     * 返回主界面
    */
    GameScene.prototype.backHome = function () {
        cc.director.loadScene('StartScene');
    };
    /**
     * 设置按钮
     */
    GameScene.prototype.setBtnClick = function () {
        var newnode = cc.instantiate(gamePrefabMgr_1.default.Instance.uiPre[gamePrefabMgr_1.uiPreName.pausepanel]);
        newnode.parent = cc.director.getScene();
    };
    /**
     * 复活
    
    reliveGame(){
        var self = this
        this.tasktime = 60
        this.taskTimeStr.string = '00:60'
        this.unscheduleAllCallbacks()
        this.schedule(()=>{
            if(PlayData.Instance.ispause) return
            self.tasktime--
            if(self.tasktime <= 0){
                let c_str = Utils.secondsFormat(self.tasktime, false)
                self.taskTimeStr.string = c_str

                self.unscheduleAllCallbacks()
                self.showGameOver(0)
            }
            else{
                let c_str = Utils.secondsFormat(self.tasktime, false)
                self.taskTimeStr.string = c_str
            }
        },1,99999,0)
    }
*/
    /**
     * 复活游戏并重新开始倒计时
     */
    GameScene.prototype.reliveGame = function () {
        var _this = this;
        this.tasktime = 60;
        this.taskTimeStr.string = this.formatTime(this.tasktime);
        this.unscheduleAllCallbacks();
        this.schedule(function () {
            if (PlayData_1.default.Instance.ispause) {
            }
            else {
                if (_this.tasktime <= 0) {
                    _this.taskTimeStr.string = _this.formatTime(0);
                    _this.unscheduleAllCallbacks();
                    _this.showGameOver(0);
                }
                else {
                    _this.tasktime--;
                    _this.taskTimeStr.string = _this.formatTime(_this.tasktime);
                }
            }
        }, 1, cc.macro.REPEAT_FOREVER);
    };
    /**
     * 格式化时间为 mm:ss 格式
     */
    GameScene.prototype.formatTime = function (seconds) {
        var minutes = Math.floor(seconds / 60);
        var secs = seconds % 60;
        return minutes.toString().padStart(2, '0') + ":" + secs.toString().padStart(2, '0');
    };
    __decorate([
        property(MapControl)
    ], GameScene.prototype, "mapcontrol", void 0);
    __decorate([
        property(cc.Sprite)
    ], GameScene.prototype, "map_png", void 0);
    __decorate([
        property(cc.SpriteFrame)
    ], GameScene.prototype, "xuankuang", void 0);
    __decorate([
        property(cc.Label)
    ], GameScene.prototype, "taskTimeStr", void 0);
    __decorate([
        property(cc.Node)
    ], GameScene.prototype, "tishinode", void 0);
    __decorate([
        property(cc.Node)
    ], GameScene.prototype, "answerPanel", void 0);
    __decorate([
        property(cc.Label)
    ], GameScene.prototype, "titlestr", void 0);
    __decorate([
        property(cc.Node)
    ], GameScene.prototype, "getAnswerBtn", void 0);
    GameScene = __decorate([
        ccclass
    ], GameScene);
    return GameScene;
}(cc.Component));
exports.default = GameScene;

cc._RF.pop();